#!/bin/bash
# 安装验证脚本 - 检查所有组件是否正确安装

echo "================================================"
echo "  生物信息数据库管理系统 - 安装验证"
echo "================================================"
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查计数
SUCCESS=0
FAILED=0
WARNING=0

check_command() {
    if command -v $1 &> /dev/null; then
        echo -e "${GREEN}✓${NC} $1 已安装: $(command -v $1)"
        ((SUCCESS++))
        if [ ! -z "$2" ]; then
            echo "  版本: $($1 $2 2>&1 | head -n 1)"
        fi
    else
        echo -e "${RED}✗${NC} $1 未安装"
        ((FAILED++))
    fi
}

check_file() {
    if [ -f "$1" ]; then
        echo -e "${GREEN}✓${NC} $1"
        ((SUCCESS++))
    else
        echo -e "${RED}✗${NC} $1 缺失"
        ((FAILED++))
    fi
}

check_dir() {
    if [ -d "$1" ]; then
        echo -e "${GREEN}✓${NC} $1"
        ((SUCCESS++))
    else
        echo -e "${RED}✗${NC} $1 缺失"
        ((FAILED++))
    fi
}

check_service() {
    if systemctl is-active --quiet $1; then
        echo -e "${GREEN}✓${NC} $1 服务运行中"
        ((SUCCESS++))
    else
        echo -e "${YELLOW}⚠${NC} $1 服务未运行"
        ((WARNING++))
    fi
}

echo "1. 检查系统依赖"
echo "-----------------------------------"
check_command python3 "--version"
check_command node "--version"
check_command npm "--version"
check_command mysql "--version"
echo ""

echo "2. 检查服务状态"
echo "-----------------------------------"
check_service mysql
check_service mariadb
echo ""

echo "3. 检查项目文件"
echo "-----------------------------------"
cd /root/dev_folder/boid_database/code

# 后端文件
echo "后端文件:"
check_file "backend/app.py"
check_file "backend/requirements.txt"
check_file "backend/config.env"

# 前端文件
echo ""
echo "前端文件:"
check_file "frontend/package.json"
check_file "frontend/vite.config.js"
check_file "frontend/index.html"
check_file "frontend/src/main.js"
check_file "frontend/src/App.vue"
check_file "frontend/src/router/index.js"
check_file "frontend/src/views/Home.vue"
check_file "frontend/src/views/Upload.vue"
check_file "frontend/src/views/History.vue"

# TUI 文件
echo ""
echo "TUI 文件:"
check_file "tui/tui.py"
check_file "tui/requirements.txt"

# 脚本文件
echo ""
echo "脚本文件:"
check_file "scripts/init_db.sh"
check_file "scripts/init_database.sql"
check_file "scripts/start_backend.sh"
check_file "scripts/start_frontend.sh"
check_file "scripts/start_tui.sh"
check_file "scripts/start_all.sh"

# 文档文件
echo ""
echo "文档文件:"
check_file "README.md"
check_file "DEPLOY.md"
check_file "QUICKSTART.md"
check_file "PROJECT_SUMMARY.md"

# 示例文件
echo ""
echo "示例文件:"
check_file "examples/sample_proteins.csv"
echo ""

echo "4. 检查目录权限"
echo "-----------------------------------"
check_dir "/var/bioinfo"
check_dir "/var/bioinfo/uploads"
check_dir "/var/bioinfo/backups"
echo ""

echo "5. 检查脚本执行权限"
echo "-----------------------------------"
for script in scripts/*.sh; do
    if [ -x "$script" ]; then
        echo -e "${GREEN}✓${NC} $script 可执行"
        ((SUCCESS++))
    else
        echo -e "${YELLOW}⚠${NC} $script 不可执行（将自动修复）"
        chmod +x "$script"
        ((WARNING++))
    fi
done
echo ""

echo "6. 检查数据库连接"
echo "-----------------------------------"
if mysql -u bioinfo_user -pbioinfo_pass -e "USE bioinfo; SHOW TABLES;" &> /dev/null; then
    echo -e "${GREEN}✓${NC} 数据库连接成功"
    echo "  数据库: bioinfo"
    echo "  用户: bioinfo_user"
    mysql -u bioinfo_user -pbioinfo_pass -e "USE bioinfo; SHOW TABLES;" | grep -v "Tables_in_bioinfo" | while read table; do
        echo "  表: $table"
    done
    ((SUCCESS++))
else
    echo -e "${YELLOW}⚠${NC} 数据库未初始化或连接失败"
    echo "  请运行: bash scripts/init_db.sh"
    ((WARNING++))
fi
echo ""

echo "================================================"
echo "  验证结果"
echo "================================================"
echo -e "${GREEN}成功: $SUCCESS${NC}"
echo -e "${YELLOW}警告: $WARNING${NC}"
echo -e "${RED}失败: $FAILED${NC}"
echo ""

if [ $FAILED -eq 0 ] && [ $WARNING -eq 0 ]; then
    echo -e "${GREEN}✓ 所有检查通过！系统已准备就绪。${NC}"
    echo ""
    echo "下一步："
    echo "  1. 如果数据库未初始化，运行: bash scripts/init_db.sh"
    echo "  2. 启动服务: bash scripts/start_all.sh"
    echo "  3. 访问: http://localhost:3000"
elif [ $FAILED -eq 0 ]; then
    echo -e "${YELLOW}⚠ 检查完成，有 $WARNING 个警告。${NC}"
    echo ""
    echo "建议操作："
    if ! systemctl is-active --quiet mysql && ! systemctl is-active --quiet mariadb; then
        echo "  - 启动 MySQL: sudo systemctl start mysql"
    fi
    echo "  - 初始化数据库: bash scripts/init_db.sh"
    echo "  - 然后运行: bash scripts/start_all.sh"
else
    echo -e "${RED}✗ 检查发现 $FAILED 个错误。${NC}"
    echo ""
    echo "请检查："
    echo "  - 是否在正确的目录: /root/dev_folder/boid_database/code"
    echo "  - 是否安装了所有必需软件: Python 3.8+, Node.js 16+, MySQL 8.0+"
    echo "  - 查看详细文档: README.md 或 DEPLOY.md"
fi

echo ""
echo "================================================"
